package cpr.command;

import java.io.File;

import cpr.DefaultCommandHandler;
import cpr.Printer;
import cpr.Rule;
import cpr.Utils;
import cpr.context.Context;
import cpr.data.HLVector;

public class CmdSubcube extends DefaultCommandHandler {
	public CmdSubcube() {
		super("subcube");
		setQuickInfo("MASTER_RULE_NUMBER SCORE [NEW_FILE_PREFIX] (former IMAGE_QUBE) extracts from current data source sub cube by given score");
	}

	public void execute(String[] params) {
		Context ctx = getCommandContext().getContext();
		int rule_nr = getParameterInteger(params,1);
		int IMAGE_SCORE = ctx.getScoreHelper().convertStringToScore(getParameter(params, 2));
		String folder = ctx.getDefaultDataFolder();
		String newFilePrefix = getParameter(params, 3, "subqube");
		if (!newFilePrefix.startsWith(File.separator)) {
			newFilePrefix = folder + newFilePrefix;
		}
		doit(rule_nr, IMAGE_SCORE, ctx.getRawDataFilePath(), newFilePrefix);
	}

	public void doit(int master_rule, int score, String originalFile, String newFilePrefix) {
		Printer.println("Warning: it works only with raw data");
		Context ctx = getCommandContext().getContext();
		Rule rule = getCommandContext().getContext().getIOContext().readRuleFromHLFile(ctx.getRawDataFilePath(), master_rule);
		int[] dec = Utils.decayScore(score, Rule.HL_SCORE);
		HLVector vector = new HLVector(dec);
		String si = getCommandContext().getContext().getPrintContext().printRuleAccordingToVectorLongOld(rule, vector);
		String suffix = "_rule_" + master_rule + "_score_" + score;
        getCommandContext().getContext().getRenumberContext().
		copyRulesFromHLFile(originalFile, newFilePrefix + suffix, si,
				vector);
	}

}
